
from fastapi import  HTTPException, Request

from starlette.middleware.base import BaseHTTPMiddleware
from web.config.Config import CONFIG


CHECK_TOKEN = CONFIG['server']['checkToken']
AUTH_TOKEN = CONFIG['server']['authToken']

class AuthMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        if CHECK_TOKEN:
            if "authToken" not in request.headers or request.headers["authToken"] != AUTH_TOKEN:
                raise HTTPException(status_code=401, detail="没有登录啦")
        response = await call_next(request)
        return response
